import json
from logger import log
from storage import Storage
from object import Object

class Map:
	def __init__(self, gameEngine, name):
		log('Loading map %s'%name)
		self.gameEngine = gameEngine
		self.data = Storage().getJSON('%s/maps/%s/meta.obj'%(Storage().get('env','dataDir'), name))
		self.objects = []
		for objectMeta in self.data['objects']:
			objectData = Storage().getJSON('%s/objects/%s/meta.obj'%(Storage().get('env','dataDir'), objectMeta['name']))
			self.objects.append(Object(objectMeta['name'], objectMeta['x'], objectMeta['y'], objectData['width'], objectData['height'], objectData['texture']))

	def addUnit(self, unit):
		self.objects.append(unit)

	def removeUnit(self, unit):
		if unit in self.objects:
			self.objects.remove(unit)

	def pollVisibility(self, what, nodeIdVisibleList):
		ins = []
		outs = []
		for object in self.objects:
			if object is what:
				continue
			print 'what: %s %s'%(what.sx, what.sy)
			print 'other: %s %s'%(object.sx, object.sy)
			if what.visible(object) and object.nodeId not in nodeIdVisibleList:
				ins.append(object)
			elif not what.visible(object) and object.nodeId in nodeIdVisibleList:
				outs.append(object)
		return (ins, outs)
